/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.util.List;

import edu.syr.iis.web.model.Conversation;
import edu.syr.iis.web.model.ConversationManager;
import edu.syr.iis.web.model.Message;
import edu.syr.iis.web.model.MessageManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * @author kinoue
 *
 */
public class MessageListController  extends AbstractController 
{
    MessageManager messageManager;
    ConversationManager conversationManager;
    
    public static final String CONV_ID = "conversationId"; 
    public static final String MSGS_KEY = "messages";
    public static final String CONV_KEY = "conversation";

    private String successView;

    @Override
	public ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response) 
    throws Exception
    {	
	ModelAndView mav = new ModelAndView(getSuccessView());
	
	List<Message> messages = null;
	Conversation conversation = null;
	
	if (request.getParameter(CONV_ID) != null
		&& request.getParameter(CONV_ID).trim().length() > 0)
	{
	    int conversationId = Integer.parseInt(request.getParameter(CONV_ID));
	    conversation = conversationManager.getConversation(conversationId);
	    messages = messageManager.getMessages(conversationId);	    
	} 	
		
	mav.addObject(CONV_KEY, conversation);
	mav.addObject(MSGS_KEY, messages);	
	
	return mav; 
    }

    public MessageManager getMessageMaager()
    {
	return messageManager;
    }

    public void setMessageManager(MessageManager messageManager)
    {
	this.messageManager = messageManager;
    }

    public String getSuccessView()
    {
	return successView;
    }

    public void setSuccessView(String successView)
    {
	this.successView = successView;
    }

    public ConversationManager getConversationManager()
    {
        return conversationManager;
    }

    public void setConversationManager(ConversationManager conversationManager)
    {
        this.conversationManager = conversationManager;
    }



}
